package com.mxchip.livestar.update

import android.app.Activity
import android.app.ProgressDialog
import android.content.Context
import android.util.Log
import com.mxchip.livestar.base.AppConstant
import com.mxchip.livestar.base.SingletonHolder
import com.mxchip.livestar.base.SystemData
import com.mxchip.livestar.base.toast
import com.mxchip.livestar.utils.Utils
import com.videohigh.updatelib.OnDownloadListener
import com.videohigh.updatelib.UpdateInfo
import com.videohigh.updatelib.UpdateManager

class UpdateInstance private constructor(context: Context) {

    companion object : SingletonHolder<UpdateInstance, Context>(::UpdateInstance)

    private val url = AppConstant.checkURL
    private val channel = "zbx"
     private val postData = "channel=${channel}&pkt_name=&version=${Utils.getVersionCode(context)}&device_id=${SystemData.userId()}"
//    private val postData = "channel=${channel}&pkt_name=&version=19&device_id=${SystemData.userId}"

    private class DefaultDialogDownloadListener(private val mContext: Context) : OnDownloadListener {
        private var mDialog: ProgressDialog? = null
        override fun onStart() {
            if (mContext is Activity && !mContext.isFinishing) {
                val dialog = ProgressDialog(mContext)
                dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL)
                dialog.setMessage("下载中...")
                dialog.isIndeterminate = false
                dialog.setCancelable(false)
                dialog.show()
                mDialog = dialog
            }
        }

        override fun onProgress(i: Int) {
            if (mDialog != null) {
                mDialog!!.progress = i
            }
        }

        override fun onFinish() {
            if (mDialog != null) {
                mDialog!!.dismiss()
                mDialog = null
            }

            ProgressDialog.show(mContext, "", "正在安装更新...")
        }
    }

    private var mInstantInstall = false

    fun setInstantInstall(instantInstall: Boolean) {
        mInstantInstall = instantInstall
    }

    fun autoCheck(context: Context) {
        UpdateManager.create(context)
                .setUrl(url)
                .setPostData(postData)
                .setWifiOnly(false)
                .setManual(false)
                .setParser {
                    Log.e("UpdateInstance autoCheck", "info string: $it")
                    val info = UpdateInfo.parse(it)
                    return@setParser info
                }.setOnDownloadListener(DefaultDialogDownloadListener(context)).check()
    }

    fun manualCheck(context: Context) {
        UpdateManager.create(context)
                .setUrl(url)
                .setPostData(postData)
                .setWifiOnly(false)
                .setManual(true)
                .setOnFailureListener {
                    context.toast("${it.message}")
                }
                .setParser {
                    Log.e("UpdateInstance manualCheck", "info string: $it")
                    val info = UpdateInfo.parse(it)
                    info.isSilent = false
                    info.isForce = false
                    return@setParser info
                }.setOnDownloadListener(DefaultDialogDownloadListener(context)).check()
    }
}